//
//  NewFeatureCollectionViewController.swift
//  Weibo
//
//  Created by 周先生 on 15/11/13.
//  Copyright © 2015年 周先生. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
//定义图片的数量
private let imageCount = 4
class NewFeatureCollectionViewController: UICollectionViewController {

    //storyboard  会自动注册cell 并指定layout
    //重写集合视图控制器的构造方法   让外界调用更加简单 不需要传参数 layout和视图的大小比比较固定
    
    init() {
        //实例化layout
        let layout = UICollectionViewFlowLayout()
        //指定layout的itemSize cell在实例化的时候就会使用这个size
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout: layout)
        
        //设置滚动分页
        collectionView?.pagingEnabled = true
        //去掉竖屏滚动条
        collectionView?.showsHorizontalScrollIndicator = false
        //隐藏弹簧效果
        collectionView?.bounces = false
        
        
    
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //手动注册cell
        self.collectionView?.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    
    }
    // MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageCount
    }
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        
        //设置背景颜色
//        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.redColor(): UIColor.blueColor()
        cell.index = indexPath.item
        return cell
    }
    
    //停止滚动的协议方法
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / screenW)
        print(page)
        if page == imageCount - 1 {
        
        //开始动画
            if let cell = collectionView?.visibleCells().last as? NewFeatureCell {
            
            cell.startAnimation()
            }
        
        }
        
    }



}
class NewFeatureCell:UICollectionViewCell {

    //显示图片的index
    var index: Int = 0 {
        didSet {
            iconImage.image = UIImage(named: "new_feature_\(index + 1)")
                        startButton.hidden = true
            
        }
    }
    
    //开始体验按钮的点击事件  需要添加@objc 否者会崩溃
    @objc private func start() {
        print("用我吧")
        //发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewController, object: nil)
        
    }
    
    //开始动画的效果
   private func startAnimation() {
    startButton.transform = CGAffineTransformMakeScale(0, 0)
    UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
        self.startButton.hidden = false
        self.startButton.transform = CGAffineTransformMakeScale(2, 2)
        }) { (_) -> Void in
            print("ok")
    }
    
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        print(frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //2,设置UI
    
    private func setupUI() {
    //添加子视图
        contentView.addSubview(iconImage)
        contentView.addSubview(startButton)
        
    //设置约束
        iconImage.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        startButton.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
            make.centerX.equalTo(contentView.snp_centerX)
        }
        //3设置点击事件
        startButton.addTarget(self, action: "start", forControlEvents: .TouchUpInside)
    }
    //1,实例化子视图控件
    private lazy var iconImage: UIImageView = UIImageView()
    //开始按钮
    private lazy var startButton: UIButton = UIButton(title: "开始体验", backImage: "new_feature_finish_button", color: nil)
        
    

}





